package tree.test112;

import listbyorder.utils.TreeNode;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/16 13:19
 */
public class Solution1 {

    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) return false;
        return helper(root, sum, root.val);
    }

    private boolean helper(TreeNode root, int sum, int cur) {
        if (root.left == null && root.right == null) return cur == sum;
        if (root.left == null) return helper(root.right, sum, cur + root.right.val);
        if (root.right == null) return helper(root.left, sum, cur + root.left.val);
        return helper(root.left, sum, cur + root.left.val) || helper(root.right, sum, cur + root.right.val);
    }
}
